%% 读取数据
data_read_args.branch_file_path="yantian402/运行方式1-branch_reordered.xlsx";
data_read_args.source_file_path="yantian402/capacity.xlsx";
data_read_args.load_file_path="yantian402/all_load_node_mapped.xlsx";
[buses, branches, substations, init_sw] = yantian_vdata(data_read_args);
Nbr = height(branches);
Nbus = height(buses);

%% 建立图模型
Edgetable = branches(:, {'bus1id', 'bus2id', 'name', 'is_sw'});
Edgetable = mergevars(Edgetable, {'bus1id', 'bus2id'}, "NewVariableName", 'EndNodes');
Edgetable.fixed = ~Edgetable.is_sw;
Edgetable.is_sw = [];
G = graph(Edgetable);

%% 设置初始运行拓扑结构
brid = findedge(G, init_sw.bus1id(~init_sw.status), init_sw.bus2id(~init_sw.status));
initial_grid = true(Nbr, 1);
initial_grid(brid) = false;

%% 设置初始拓扑中所有在运支路故障，选择，上优化求解器
fault_br_eid=find(initial_grid);
fL=numel(fault_br_eid);
sol = cell(fL, fL);
fault_br_name=strings(fL, fL);

for k = 2:fL
    parfor m = 1:k-1
        try
            fault_br_name(k,m)=compose("%s-&-%s",G.Edges.name(fault_br_eid(k)),G.Edges.name(fault_br_eid(m)));
            sol{k,m} = reconstruct_branch_n_fault(G, [fault_br_eid(k),fault_br_eid(m)], initial_grid, buses, branches, substations);
        catch
            fault_br_name(k,m)="";
            sol{k,m}=[];
        end
    end
end
fault_br_name=fault_br_name(fault_br_name~="");
fun=@(x) ~isempty(x);
idx = cellfun(fun,sol);
sol=sol(idx);

%% 存储结果
save solution2.mat sol initial_grid G fault_br_name;

